package common

import (
	"image/color"
	"strconv"
)

// 将16进制颜色转换为RGB
func HexToRGB(hex string) color.RGBA {
	if hex[0] == '#' {
		hex = hex[1:]
	}
	var r, g, b uint64
	var err error
	if len(hex) == 3 {
		r, err = strconv.ParseUint(string(hex[0])+string(hex[0]), 16, 32)
		g, err = strconv.ParseUint(string(hex[1])+string(hex[1]), 16, 32)
		b, err = strconv.ParseUint(string(hex[2])+string(hex[2]), 16, 32)
	}
	if len(hex) == 6 {
		r, err = strconv.ParseUint(hex[0:2], 16, 32)
		g, err = strconv.ParseUint(hex[2:4], 16, 32)
		b, err = strconv.ParseUint(hex[4:6], 16, 32)
	}
	if err != nil {
		return color.RGBA{}
	}
	return color.RGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: 255}
}
